package com.example.an.mymusic;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by an on 2017/10/18.
 */
public class FindSongs {
public List<Mp3info>getMp3info(ContentResolver contentResolver){
    Cursor cursor=contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    List<Mp3info> mp3Infos=new ArrayList<Mp3info>();
    for (int i=0;i<cursor.getCount();i++){
        Mp3info mp3Info = new Mp3info();
        cursor.moveToNext(); //新建一个歌曲对象,将从cursor里读出的信息存放进去,直到取完cursor里面的内容为止.
        long id=cursor.getLong(cursor
                .getColumnIndex(MediaStore.Audio.Media._ID));
        String artist = cursor.getString(cursor
                .getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家
        String title = cursor.getString((cursor
                .getColumnIndex(MediaStore.Audio.Media.TITLE)));//音乐标题
        long duration = cursor.getLong(cursor
                .getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
        long size = cursor.getLong(cursor
                .getColumnIndex(MediaStore.Audio.Media.SIZE));  //文件大小
        String url = cursor.getString(cursor
                .getColumnIndex(MediaStore.Audio.Media.DATA));  //文件路径
        String album = cursor.getString(cursor
                .getColumnIndex(MediaStore.Audio.Media.ALBUM)); //唱片图片
        long album_id = cursor.getLong(cursor
                .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); //唱片图片ID
        int isMusic = cursor.getInt(cursor
                .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
        //只把1分钟以上的音乐添加到集合当中
        if(isMusic!=0&&duration / (1000*60)>=1){
            mp3Info.setId(id);
            mp3Info.setTitle(title);
            mp3Info.setArtist(artist);
            mp3Info.setDuration(duration);
            mp3Info.setSize(size);
            mp3Info.setUrl(url);
            mp3Info.setAlbum(album);
            mp3Info.setAlbum_id(album_id);
            mp3Infos.add(mp3Info);

        }
    }
    return  mp3Infos;
}
    public void setListAdpter(Context context,List<Mp3info> mp3infos,ListView mMusicList){
        List<HashMap<String,String>> mp3List=new ArrayList<HashMap<String, String>>();
        MusicListAdapter mAdapter=new MusicListAdapter(context,mp3infos);
        mMusicList.setAdapter(mAdapter);
    }
}
